const {mysql} = require('../qcloud')

module.exports = async (ctx) => {
    const {id, openId} = ctx.request.query

    const goodsInfo = await mysql('nideshop_goods').where({id}).select()

    const gallery = await mysql('nideshop_goods_gallery').where({
        goods_id: id
    }).select()

    // 关联查询两个表
    const attribute = await mysql('nideshop_goods_attribute')
        .column('nideshop_goods_attribute.value', 'nideshop_attribute.name')
        .leftJoin('nideshop_attribute', 'nideshop_goods_attribute.attribute_id', 'nideshop_attribute.id')
        .where({'nideshop_goods_attribute.goods_id': id})
        .select()

    const FAQ = await mysql('nideshop_goods_issue').select()

    const cartCount = await mysql('nideshop_cart').where('open_id', openId).column('goods_id').select()

    ctx.state.data = {
        goodsInfo: goodsInfo[0],
        gallery,
        attribute,
        FAQ,
        cartCount: cartCount.length
    }
}
